package com.gxa.saas.service.impl;

import com.gxa.saas.mapper.GoodsMapper;
import com.gxa.saas.pojo.Goods;
import com.gxa.saas.pojo.VisitResult;
import com.gxa.saas.service.GoodsService;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @author CZJ
 * @date 2022/6/9 15:38
 */
@Service
@Transactional(rollbackFor = {Throwable.class})
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public List<Goods> selectByPage(Integer page, Integer size) {
        return goodsMapper.selectByPage((page-1)*size,size);
    }

    @Override
    public Integer insert(Goods goods) {
        return goodsMapper.insert(goods);
    }

    @Override
    public Integer update(Goods goods) {
        return goodsMapper.update(goods);
    }

    @Override
    public Integer delete(Integer id) {
        return goodsMapper.delete(id);
    }

    @Override
    public List<Goods> selectAll() {
        return goodsMapper.selectAll();
    }

    @Override
    public List<Goods> selectGoodsLow(Goods goods,Integer page, Integer size) {
        List<Goods> goods1 = goodsMapper.selectGoodsLow(goods);
        int totalCount = goods1.size();
        int pageCount = 0;
        List<Goods> subList;
        int m = totalCount % size;
        if (m > 0) {
            pageCount = totalCount / size + 1;
        } else {
            pageCount = totalCount / size;
        }
        if (m == 0) {
            subList = goods1.subList((page - 1) * size, size * (page));
        } else {
            if (page == pageCount) {
                subList = goods1.subList((page - 1) * size, totalCount);
            } else {
                subList = goods1.subList((page - 1) * size, size * (page));
            }


        }
        return subList;
    }

    @Override
    public List<Goods> selectGoodsLow1(Goods goods) {
        return goodsMapper.selectGoodsLow(goods);
    }


    @Override
    public List<Goods> selectBySort(Boolean boo) {
        if (boo){
            return goodsMapper.ascendingOrder();
        }else {
            return goodsMapper.descendingOrder();
        }
    }


    @Override
    public void export(List<Goods> list, HttpServletResponse response) throws Exception {
        HSSFWorkbook workbook = new HSSFWorkbook();

        HSSFSheet sheet = workbook.createSheet("商品列表");
        String fileName = "商品列表.xls";

        fileName = URLEncoder.encode(fileName,"utf-8").replaceAll("\\+", "%20");

        String[] headers = { "序号","商品编号", "商品名称", "成本价格","规格","厂商名称","品牌名称","商品类型","大单位","小单位","上架状态"};

        HSSFRow row = sheet.createRow(0);
        for(int i=0;i<headers.length;i++){
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }

        int rowNum = 1;
        for (int i = 0; i < list.size(); i++) {
            HSSFRow dataRow = sheet.createRow(rowNum);
            dataRow.createCell(0).setCellValue(rowNum);
            dataRow.createCell(1).setCellValue(list.get(i).getId());
            dataRow.createCell(2).setCellValue(list.get(i).getName());
            dataRow.createCell(3).setCellValue(list.get(i).getCostPrice());
            dataRow.createCell(4).setCellValue(list.get(i).getNorms());
            dataRow.createCell(5).setCellValue(list.get(i).getFactory());
            dataRow.createCell(6).setCellValue(list.get(i).getBrandName());
            dataRow.createCell(7).setCellValue(list.get(i).getGoodsType());
            dataRow.createCell(8).setCellValue(list.get(i).getBigUnit());
            dataRow.createCell(9).setCellValue(list.get(i).getSmallUnit());
            dataRow.createCell(10).setCellValue(list.get(i).getIsUsed());
            rowNum++;
        }

        //返回给前端下载
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());
    }

    @Override
    public List<Goods> selectGoodsHigh(Goods goods,Integer page, Integer size) {
        List<Goods> goods1 = goodsMapper.selectGoodsHigh(goods);
        int totalCount = goods1.size();
        int pageCount = 0;
        List<Goods> subList;
        int m = totalCount % size;
        if (m > 0) {
            pageCount = totalCount / size + 1;
        } else {
            pageCount = totalCount / size;
        }
        if (m == 0) {
            subList = goods1.subList((page - 1) * size, size * (page));
        } else {
            if (page == pageCount) {
                subList = goods1.subList((page - 1) * size, totalCount);
            } else {
                subList = goods1.subList((page - 1) * size, size * (page));
            }
        }
        return subList;
    }

    @Override
    public List<Goods> selectGoodsHigh1(Goods goods) {
        return goodsMapper.selectGoodsHigh(goods);
    }


    @Override
    public List<Goods> selectTwo() {
        return goodsMapper.selectTwo();
    }
}
